//
//  TanHunMainViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "TanHunHeaderView.h"
#import "GKPhotoBrowser.h"

#import "PostEvaluateCell.h"
#import "PostEvaluateLookMoreCell.h"

#import "PostSectionHeaderView.h"
#import "PostSectionFooterView.h"
#import "BA_CommunityInputTextView.h"

#import "PostListViewController.h"
#import "SearchResultSegViewController.h"
#import "PostDetailViewController.h"
#import "RelatedTopicViewController.h"

#import "TanHunMainViewController.h"

@interface TanHunMainViewController ()<UITableViewDelegate, UITableViewDataSource, GKPhotoBrowserDelegate, BA_BottomInputViewDelegate, UITextViewDelegate>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) TanHunHeaderView *headerView;

@property (nonatomic, assign) NSInteger selectIndex;


@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) NSMutableArray <CommonPostModel*>* dataArray;
@property (nonatomic, strong) NSMutableArray <CommunityThemeModel*>* themeArray;

@property (nonatomic, strong) BA_CommunityInputTextView *footView;

@property (nonatomic, strong) UIButton *topBtn;
@property (nonatomic, copy) NSString * remark;

@end

@implementation TanHunMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self.footView];
    [self.footView hideView];
    self.topBtn.hidden = YES;

    self.remark = @"2";
    [self refreshParam];
    [self request_data];

    [self initViewAction];

}

- (void)refreshParam {
    self.pageSize = 10;
    self.pageNum = 1;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    [[IQKeyboardManager sharedManager] setEnable:NO];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
    [[IQKeyboardManager sharedManager] setEnable:YES];

    [self.footView hideView];
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    [self.footView hideView];
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
    
    [self.footView hideView];
    return YES;
}

-(void)BA_BottomInputViewSendTextMessage:(NSString *)message {
    [self request_postEvaWithIndex:self.selectIndex AndContent:message];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.tag == 10086) {
        self.offset_Y = scrollView.contentOffset.y;
    }
    
    if (scrollView.contentOffset.y>TTScreenHeight) {
        self.topBtn.hidden = NO;
    }else{
        self.topBtn.hidden = YES;
    }
}

- (void)request_data {
    
    [SVProgressHUD showWithStatus:@"正在请求数据"];
    co_launch(^{
        await([self co_request_themeList]);
        await([self co_request_postList]);
        NSError *error = co_getError();
        [SVProgressHUD dismiss];
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        if (error) {
        } else {
            [self.headerView fillWith:self.themeArray];
            [self.tableview reloadData];
        }
    });
}

- (void)initViewAction {
    
    WEAKSELF
    self.headerView.TanHunHeaderViewBlock = ^(TanHunHeaderViewActionType type, NSInteger index) {
      
        switch (type) {
            case TanHunHeaderViewActionType_tiezi:{
                
                if (index == 0) {
                    
                    RelatedTopicViewController *vc = [[RelatedTopicViewController alloc] init];
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                    
                } else {
                    PostListViewController *vc = [[PostListViewController alloc] init];
                    vc.tid = weakSelf.themeArray[index].themeId;
                    NSLog(@"%@",vc.tid);
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }

            } break;
            case TanHunHeaderViewActionType_wenda:{
                
                SearchResultSegViewController *vc = [[SearchResultSegViewController alloc] init];
                vc.selectIndex = SearchResultSegType_post;
                [weakSelf.navigationController pushViewController:vc animated:YES];

                
            } break;
            case TanHunHeaderViewActionType_hot:{
                weakSelf.remark = @"2";
                [weakSelf refreshParam];
                [weakSelf request_data];
            } break;
            case TanHunHeaderViewActionType_new:{
                weakSelf.remark = @"3";
                [weakSelf refreshParam];
                [weakSelf request_data];
            } break;
            default:
                break;
        }
    };
}

- (void)goTop {
    [self.tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}
    
- (void)showImageWithArray:(NSArray *)imageUrls index:(NSInteger)index {
    NSMutableArray *photos = [NSMutableArray new];
    
    [imageUrls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:obj];
        [photos addObject:photo];
    }];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
    
    browser.delegate  = self;
    [browser showFromVC:self];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //判断显示几个评论
    NSInteger count = 0;
    if (self.dataArray[section].sysPostChildList.count > 2) {
        count = 2;
    } else {
        count = self.dataArray[section].sysPostChildList.count;
    }
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.dataArray[section].itemHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row < 2) {
        return self.dataArray[indexPath.section].sysPostChildList[indexPath.row].itemHeight + 10;
    } else {
        return [PostEvaluateLookMoreCell calculateHeight];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.dataArray[section].sysPostChildList.count > 0) {
        return 45;
    } else {
        return 15;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        
    PostSectionHeaderView *sectionHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PostSectionHeaderView"];
    sectionHeader.contentView.backgroundColor = UIColor.whiteColor;
    WEAKSELF
    sectionHeader.PostSectionHeaderViewBlock = ^(PostSectionHeaderActionType type, NSInteger index) {
      
        switch (type) {
            case PostSectionHeaderActionType_userIcon:{
                if (weakSelf.dataArray[section].userHeadUrl.length > 0) {
                    [weakSelf showImageWithArray:@[weakSelf.dataArray[section].userHeadUrl] index:index];
                }
            } break;
            case PostSectionHeaderActionType_image:{
                if (weakSelf.dataArray[section].photoArray.count > 0) {
                    [weakSelf showImageWithArray:weakSelf.dataArray[section].photoArray index:index];
                }
            } break;
            case PostSectionHeaderActionType_theme:{

                PostListViewController *vc = [[PostListViewController alloc] init];
                vc.tid = weakSelf.dataArray[section].themeId;
                [weakSelf.navigationController pushViewController:vc animated:YES];

            } break;
            case PostSectionHeaderActionType_eva:{
                
                weakSelf.selectIndex = section;
                [weakSelf.footView showView];
                [weakSelf.footView.textView becomeFirstResponder];
                
            } break;
            case PostSectionHeaderActionType_thumb:{
                
                weakSelf.selectIndex = section;
                [weakSelf request_postThumbWithIndex:section];

            } break;
            default:
                break;
        }
        
    };
    
    [sectionHeader fillWithModel:self.dataArray[section]];
    
    [sectionHeader addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        PostDetailViewController *vc = [[PostDetailViewController alloc] init];
        vc.pid = weakSelf.dataArray[section].pid;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    return sectionHeader;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    PostSectionFooterView*sectionFooter = [[NSBundle mainBundle] loadNibNamed:@"PostSectionFooterView" owner:nil options:nil].firstObject;
    sectionFooter.contentView.backgroundColor = UIColor.whiteColor;

    sectionFooter.mainTitleLab.text = [NSString stringWithFormat:@"查看共%lu条评论",(unsigned long)self.dataArray[section].replyCount];
    
    WEAKSELF
    [sectionFooter addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        PostDetailViewController *vc = [[PostDetailViewController alloc] init];
        vc.pid = weakSelf.dataArray[section].pid;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    return sectionFooter;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PostEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostEvaluateCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillWithModel:self.dataArray[indexPath.section].sysPostChildList[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    PostDetailViewController *vc = [[PostDetailViewController alloc] init];
    vc.pid = self.dataArray[indexPath.section].pid;
    [self.navigationController pushViewController:vc animated:YES];
}

- (BA_CommunityInputTextView *)footView {
    if (_footView == nil) {
        _footView = [[BA_CommunityInputTextView alloc]init];
        _footView.delegate = self;
        _footView.textView.delegate = self;
    }
    return _footView;
}

- (UIButton *)topBtn {
    
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topBtn setImage:[UIImage imageNamed:@"to_top"] forState:UIControlStateNormal];
        [_topBtn addTarget:self action:@selector(goTop) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topBtn;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        _tableview.tag = 10086;
        [_tableview registerNib:[UINib nibWithNibName:@"PostEvaluateCell" bundle:nil] forCellReuseIdentifier:@"PostEvaluateCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"PostEvaluateLookMoreCell" bundle:nil] forCellReuseIdentifier:@"PostEvaluateLookMoreCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"PostSectionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"PostSectionHeaderView"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"PostSectionFooterView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"PostSectionFooterView"];

        
        [self.view addSubview:_tableview];
        
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];

        self.headerView.frame = CGRectMake(0, 0, TTScreenWidth, [TanHunHeaderView calculateHeight]);
        _tableview.tableHeaderView = self.headerView;
        self.headerView.autoresizingMask = UIViewAutoresizingNone;

        [self.view addSubview:self.topBtn];
        [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-6);
            make.bottom.mas_equalTo(- 20);
        }];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf refreshParam];
            [weakSelf request_data];
        }];

        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum ++;
            [weakSelf request_data];
        }];
        
    }
    return _tableview;
}
    
- (TanHunHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[NSBundle mainBundle] loadNibNamed:@"TanHunHeaderView" owner:nil options:nil].firstObject;
    }
    return _headerView;
}

- (COPromise<id> *)co_request_postList {
    
    return [COPromise promise:^(COPromiseFulfill  _Nonnull fullfill, COPromiseReject  _Nonnull reject) {
        dispatch_async(dispatch_get_main_queue(), ^{

            NSMutableDictionary *dic = @{}.mutableCopy;
            NSMutableDictionary *t = @{}.mutableCopy;

            [dic setValue:@(self.pageNum) forKey:@"pageNum"];
            [dic setValue:@(self.pageSize) forKey:@"pageSize"];
            
            [t setValue:[TTUserModel sharedObject].userId forKey:@"replyUserId"];
            
            [t setValue:@"2" forKey:@"isDel"];
            [t setValue:@"1" forKey:@"type"];
            [t setValue:@"3" forKey:@"status"];
            [t setValue:self.remark forKey:@"remark"];
            [dic setValue:t forKey:@"t"];

            NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SysPosts];
            
            WEAKSELF
            [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
                
                [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
                if ([data isKindOfClass:[NSDictionary class]]) {

                    //请求成功
                    NSInteger code = [[data valueForKey:@"code"] integerValue];
                    if (code == 10000) {
                        NSDictionary *resultDic = [data valueForKey:@"data"];
                        
                        if ([resultDic isKindOfClass:[NSDictionary class]]) {
                            
                            NSArray *list = [resultDic valueForKey:@"list"];
                            
                            if ([list isKindOfClass:[NSArray class]]) {
                                
                                if (weakSelf.pageNum == 1) {
                                    weakSelf.dataArray = @[].mutableCopy;
                                }
                                    
                                for (NSDictionary *dic in list) {
                                    CommonPostModel *model = [CommonPostModel modelWithDictionary:dic];
                                    
                                    NSArray *sysMediaList = [dic valueForKey:@"sysMediaList"];
                                    if ([sysMediaList isKindOfClass:[NSArray class]]) {
                                        model.sysMediaList = @[].mutableCopy;
                                        model.photoArray = @[].mutableCopy;
                                        for (id mediaDic in sysMediaList) {
                                            
                                            CommonMediaModel *mediaModel = [[CommonMediaModel alloc] init];
                                            if ([mediaDic isKindOfClass:[NSDictionary class]]) {
                                                NSDictionary *media = mediaDic;
                                                mediaModel = [CommonMediaModel modelWithDictionary:media];
                                                [model.photoArray addObject:mediaModel.url];
                                            }
                                            
                                            if ([mediaDic isKindOfClass:[NSString class]]) {
                                                NSString *media = mediaDic;
                                                mediaModel.url = media;
                                                [model.photoArray addObject:media];
                                            }
                                            [model.sysMediaList addObject:mediaModel];
                                        }
                                    }
                                    
                                    NSArray *sysPostChildList = [dic valueForKey:@"sysPostChildList"];

                                    if ([sysPostChildList isKindOfClass:[NSArray class]]) {
                                        //最多展示两条，所以就不需要那么多循环  省点时间
                                        if (sysPostChildList.count > 2) {
                                            sysPostChildList = [sysPostChildList subarrayWithRange:NSMakeRange(0, 2)];
                                        }
                                        
                                        model.sysPostChildList = @[].mutableCopy;
                                        for (NSDictionary * evaDic in sysPostChildList) {
                                            CommunityEvaModel *evaModel = [CommunityEvaModel modelWithDictionary:evaDic];
                                            
                                            //富文本
                                            NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:%@",evaModel.userNickName,evaModel.content]];
                                            NSRange nameRange = NSMakeRange(0, evaModel.userNickName.length);
                                            NSRange contentRange = NSMakeRange(evaModel.userNickName.length + 1,evaModel.content.length);
                                            // 设置字体大小
                                            [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:nameRange];
                                            [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:contentRange];
                                            evaModel.evaContentAttr = attrStr;
                                            
                                            evaModel.content = [NSString stringWithFormat:@"%@:%@",evaModel.userNickName,evaModel.content];
                                            
                                            CGFloat itemHeight = [Utilities getStringHeightWithText:evaModel.content font:[UIFont systemFontOfSize:13] viewWidth:(TTScreenWidth-48)];
                                            
                                            if (itemHeight > 30) {
                                                evaModel.itemHeight = 30;
                                            } else {
                                                evaModel.itemHeight = [Utilities getStringHeightWithText:evaModel.content font:[UIFont systemFontOfSize:13] viewWidth:(TTScreenWidth-48)];
                                            }

                                            [model.sysPostChildList addObject:evaModel];
                                        }
                                    }
                                    
                                    model.content = [NSString stringWithFormat:@"【%@】%@",model.themeTitle,model.title];
                                    model.textHeight = [Utilities getStringHeightWithText:model.content font:[UIFont systemFontOfSize:14] viewWidth:(TTScreenWidth - 28)];
                                    
                                    if (model.sysMediaList.count > 0) {
                                        model.imageHeight = (TTScreenWidth - 32)/3 + 10;
                                    } else {
                                        model.imageHeight = CGFLOAT_MIN;
                                    }
                                    model.itemHeight = [PostSectionHeaderView calculateHWithModel:model];
                                    
                                    [weakSelf.dataArray addObject:model];
                                }
                            } else {
                                
                            }
                        }
                    }
                }
                fullfill(weakSelf.dataArray);
            } FailedBlock:^(NSError * _Nonnull error) {
                reject(error);
            }];
        });
    }];
}

- (COPromise<id> *)co_request_themeList {
    
    return [COPromise promise:^(COPromiseFulfill  _Nonnull fullfill, COPromiseReject  _Nonnull reject) {
        dispatch_async(dispatch_get_main_queue(), ^{

            NSMutableDictionary *dic = @{}.mutableCopy;
            [dic setValue:@"1" forKey:@"isDisabled"];
            NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ThemeList];
            WEAKSELF
            [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

                if ([data isKindOfClass:[NSDictionary class]]) {
                    //请求成功
                    NSInteger code = [[data valueForKey:@"code"] integerValue];
                    if (code == 10000) {
                        NSArray *resultArr = [data valueForKey:@"data"];
                        
                        if ([resultArr isKindOfClass:[NSArray class]]) {
                            weakSelf.themeArray = @[].mutableCopy;
                            //首个标签写死的
                            CommunityThemeModel *otherModel = [CommunityThemeModel modelWithDictionary:dic];
                            otherModel.themeTitle = @"主题贴吧";
                            otherModel.isOther = YES;
                            otherModel.index = -1;
                            otherModel.themeUrl = B_THEMEPOST_INDEX_TAG;
                            [weakSelf.themeArray addObject:otherModel];
                            
                            for (int i = 0; i < resultArr.count; i++) {
                                CommunityThemeModel *model = [CommunityThemeModel modelWithDictionary:resultArr[i]];
                                model.isOther = NO;
                                model.index = i;
                                [weakSelf.themeArray addObject:model];
                            }
                        }
                    }
                }
                fullfill(weakSelf.themeArray);
            } FailedBlock:^(NSError * _Nonnull error) {
                reject(error);
            }];
        });
    }];
}

- (void)request_postEvaWithIndex:(NSInteger)index AndContent:(NSString *)content {
        
    CommonPostModel *model = self.dataArray[index];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:content forKey:@"content"];

    [dic setValue:model.pid forKey:@"baseId"];
    [dic setValue:@"2" forKey:@"level"];
    [dic setValue:model.source forKey:@"source"];
    [dic setValue:model.userId forKey:@"userId"];
    
    //操作类型（1：评论，2：点赞，3：删除，4：下架，5：取消点赞）必传
    [dic setValue:@"1" forKey:@"mold"];

    //类型（1：谈婚，2：论嫁，3：问答）必传
    [dic setValue:@"1" forKey:@"type"];
    //层级（1,2,3）必传
    [dic setValue:@"2" forKey:@"replySource"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"replyUserId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_CommonEva];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在发布评论"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                weakSelf.footView.textView.text = @"";
                [weakSelf.view endEditing:YES];
                
                [weakSelf request_data];
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}


- (void)request_postThumbWithIndex:(NSInteger)index {
        
    CommonPostModel *model = self.dataArray[index];

    NSMutableDictionary *dic = @{}.mutableCopy;

    //点赞帖子
    [dic setValue:model.pid forKey:@"baseId"];
    [dic setValue:@"1" forKey:@"level"];
    [dic setValue:model.source forKey:@"source"];
    [dic setValue:model.userId forKey:@"userId"];

    
    //操作类型（1：评论，2：点赞，3：删除，4：下架，5：取消点赞）必传
    if (model.isZan.length > 0) {
        [dic setValue:@"5" forKey:@"mold"];
        [dic setValue:model.isZan forKey:@"otherId"];
    } else {
        [dic setValue:@"2" forKey:@"mold"];
    }

    //类型（1：谈婚，2：论嫁，3：问答）必传
    [dic setValue:@"1" forKey:@"type"];
    //层级（1,2,3）必传
    [dic setValue:@"2" forKey:@"replySource"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"replyUserId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_CommonEva];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
        
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSString *isZan = [data valueForKey:@"data"];
                if ([isZan isKindOfClass:[NSString class]]) {
                    
                    model.isZan = isZan;
                    if (isZan.length > 0) {
                        model.likes++;
                    } else {
                        model.likes--;
                    }
                    
                    
//                    weakSelf.dataArray[index].isZan = isZan;
//                    if (isZan.length > 0) {
//                        weakSelf.dataArray[index].likes++;
//                    } else {
//                        weakSelf.dataArray[index].likes--;
//                    }
                    [weakSelf.tableview reloadData];
                }
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {

    }];
}

@end
